﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using ProjectManageSystemApi.Models;
using ProjectManageSystemApi.Models.WorkerType;
using ProjectManageSystemApi.Services;
using ProjectManageSystemApi.Utilities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ProjectManageSystemApi.Controllers
{
    /// <summary>
    /// 工种类型
    /// </summary>
    [Authorize]
    [Route("api/[controller]")]
    [ApiController]
    [ApiExplorerSettings(GroupName = Grouping.GroupName_v1)]
    public class WorkerTypeInfoController : Controller
    {
        readonly IWorkerTypeInfoServices _workerTypeInfo;
        public WorkerTypeInfoController(IWorkerTypeInfoServices workerTypeInfo)
        {
            _workerTypeInfo = workerTypeInfo;
        }

        [HttpPost, Route("Add")]
        public ApiResult Add([FromBody] WorkerTypeInfoDto workerType)
        {
            if (workerType == null
                || workerType.Name.IsNullOrEmpty())
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }

            // 判断名称是否存在
            if (_workerTypeInfo.ExistByName(workerType.Name))
            {
                return ApiResultStatus.Fail(ApiResultCodes.WORKER_TYPE_INFO_NAME_EXIST);
            }

            // 增加数据
            bool r = _workerTypeInfo.Add(workerType);
            return ApiResultStatus.Create(r, ApiResultCodes.OPERATION_FAILED);
        }

        [HttpPost, Route("Update")]
        public ApiResult Update([FromBody] WorkerTypeInfoDto workerType)
        {
            if (workerType == null
                || !workerType.Id.NumberGtZero()
                || workerType.Name.IsNullOrEmpty())
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }

            // 判断id 是否存在
            if (!_workerTypeInfo.ExistById(workerType.Id))
            {
                return ApiResultStatus.Fail(ApiResultCodes.WORKER_TYPE_INFO_ID_NOT_EXIST);
            }

            // 判断name是否存在
            if (_workerTypeInfo.ExistByName(workerType.Name, workerType.Id))
            {
                return ApiResultStatus.Fail(ApiResultCodes.WORKER_TYPE_INFO_NAME_EXIST);
            }

            // 修改数据
            bool r = _workerTypeInfo.Update(workerType);
            return ApiResultStatus.Create(r, ApiResultCodes.OPERATION_FAILED);
        }

        [HttpPost, Route("Delete")]
        public ApiResult Delete(uint id)
        {
            if (!id.NumberGtZero())
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }

            // 判断id 是否存在
            if (!_workerTypeInfo.ExistById(id))
            {
                return ApiResultStatus.Fail(ApiResultCodes.WORKER_TYPE_INFO_ID_NOT_EXIST);
            }

            // 删除数据
            bool r = _workerTypeInfo.Delete(id);
            return ApiResultStatus.Create(r, ApiResultCodes.OPERATION_FAILED);
        }
        
        [HttpPost, Route("Query")]
        public ApiResult Query([FromBody]Models.PageBo<WorkerTypeParams> pageBo)
        {
            pageBo = pageBo.Default();
            var result = _workerTypeInfo.Query(pageBo);
            return ApiResultStatus.Success(result);
        }

        [HttpGet, Route("QueryAll")]
        public ApiResult QueryAll()
        {
            var pageBo = ApiUtilities.Default<WorkerTypeParams>(null);
            pageBo.Size = int.MaxValue;
            var result = _workerTypeInfo.Query(pageBo);
            return ApiResultStatus.Success(result);
        }
    }
}
